package przykladyWykresow;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Random;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

@SuppressWarnings("serial")
public class WykresLiniowy extends ChartPanelAbstract {

	private String title = null;
	private static XYSeriesCollection set1;

	static HashMap<String, Integer> hashmap1 = new LinkedHashMap<String, Integer>();

	static {
		for (int j = 0; j < 8; j++) {
			hashmap1.put("" + j, random(40));
		}
	}

	public WykresLiniowy(final String _title) {
		super(createChart(_title));
		setTitle(_title);
	}

	private static void createDataset() {
		set1 = new XYSeriesCollection();
		final XYSeries sin1 = new XYSeries("sin1");

		for (String key : hashmap1.keySet()) {
			sin1.add(new BigDecimal(key), hashmap1.get(key));
		}

		set1.addSeries(sin1);
	}

	public static JFreeChart createChart(String _title) {
		createDataset();
		final JFreeChart chart = ChartFactory.createXYLineChart(_title, "X",
				"Y", set1, PlotOrientation.VERTICAL, true, true, false);

		return chart;

	}

	@Override
	public void changeData() {
		this.setChart(createChart(getTitle()));
	}

	@Override
	public String getTitle() {
		return this.title;
	}

	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	@Override
	public HashMap<String, Integer> getHashMapDataSet() {
		return hashmap1;
	}

	@Override
	public void setHashMapDataSet(HashMap<String, Integer> hashMap) {
		if (hashMap != null) {
			hashmap1 = hashMap;
		}
	}

	private static int random(int max) {
		return new Random().nextInt(max);
	}
}
